package com.foreverwin.mes.common.controller;

import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mes.common.service.PrintLogService;
import com.foreverwin.mes.common.model.PrintLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 *
 * @author Ervin Chen
 * @since 2020-03-06
 */
@Controller
@RequestMapping("/printLogs")
public class PrintLogController {
    private final Logger logger = LoggerFactory.getLogger(PrintLogController.class);

    @Autowired
    public PrintLogService printLogService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public AjaxResult getPrintLogById(@PathVariable String id) {
        PrintLog result;
        try {
            result = printLogService.selectById(id);
        } catch (Exception e) {
            logger.error("getPrintLogById -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public AjaxResult getPrintLogList(PrintLog printLog){
        List<PrintLog> result;
        try {
            EntityWrapper<PrintLog> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(printLog);
            result = printLogService.selectList(EntityWrapper);
        } catch (Exception e) {
            logger.error("getPrintLogList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/getPrintLogPageList")
    public AjaxResult getPrintLogPageList(FrontPage<PrintLog> frontPage, PrintLog printLog){
        Page result;
        try {
            String site = CommonMethods.getSite();
            printLog.setSite(site);
            EntityWrapper<PrintLog> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(printLog);
            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                //TODO modify global query
                EntityWrapper
                    .like("handle", frontPage.getGlobalQuery())
                    .or().like("site", frontPage.getGlobalQuery())
                    .or().like("printer", frontPage.getGlobalQuery())
                    .or().like("printTemplate", frontPage.getGlobalQuery())
                    .or().like("printParam", frontPage.getGlobalQuery())
                    .or().like("printServer", frontPage.getGlobalQuery())
                    .or().like("status", frontPage.getGlobalQuery())
                    .or().like("errorMsg", frontPage.getGlobalQuery())
                    .or().like("isReprint", frontPage.getGlobalQuery())
                    .or().like("createUserBo", frontPage.getGlobalQuery())
                    .or().like("updateUserBo", frontPage.getGlobalQuery())
        ;
            }
            result = printLogService.selectPage(frontPage.getPagePlus(), EntityWrapper);
        } catch (Exception e) {
            logger.error("getPrintLogPageList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 保存和修改公用
     * @param printLog  传递的实体
     * @return  null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_save")
    public AjaxResult printLogSave(@RequestBody PrintLog printLog) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        int count;
        try {
            count = printLogService.insertOrUpdate(printLog) ? 1 : 0;
        } catch (Exception e) {
            logger.error("printLogSave -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(count == 0 ? null : printLog);
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public AjaxResult printLogDelete(@PathVariable("id") String id){
        int count;
        try {
            count = printLogService.deleteById(id) ? 1 : 0;
        } catch (Exception e) {
            logger.error("printLogDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_batchDelete")
    public AjaxResult removeByIds(List<String> ids){
        int count;
        try {
            count = printLogService.deleteBatchIds(ids) ? 1 : 0;
        } catch (Exception e) {
            logger.error("printLogBatchDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }
}